import {
  scBanner,
  professionData,
  shicaoRecommend,
  professionNews,
  researchCenter,
  specialTopicList
} from '@/api/sc_home'
import tool from '@/utils/tool'
export default {
  namespaced: true,
  state: () => ({
    profession_id: 21,
    banners: [],
    professionData: {},
    kjsc_menus: [
      {},
      {
        title: '财税政策',
        en: 'policy',
        nav_id: 11
      },
      {
        title: '热点干货',
        en: 'infos',
        nav_id: 10
      },
      {
        title: '会计实务',
        en: 'kjsw',
        nav_id: 18
      },
      {
        title: '就业指导',
        en: 'employment',
        nav_id: 19
      },
      {
        title: '实操问答',
        en: 'answer',
        nav_id: 20
      }
    ],
    shicaoRecommendList: [],
    // 热点干货

    infos: {
      list: [],
      name: '热点干货',
      en: 'infos',
      nav_id: 10
    },
    // 财税政策
    policy: {
      list: [],
      name: '财税政策',
      en: 'policy',
      nav_id: 11
    },
    kjsw: {
      list: [],
      name: '会计实务',
      en: 'kjsw',
      nav_id: 18
    },
    employment: {
      list: [],
      name: '就业指导',
      en: 'employment',
      nav_id: 19
    },
    answer: {
      list: [],
      name: '实操问答',
      en: 'answer',
      nav_id: 20
    },
    researchCenterList: [],
    subjects: {
      1: {
        title: '初级会计职称',
        name: '初级会计',
        courseName: '初级会计',
        en: 'cjkj',
        short: '初级',
        color: '#03a9f4',
        icon: '初',
        iconfont: '#iconchujihuiji',
        profession_id: '4',
        planName: '初级会计'
      },
      2: {
        title: '中级会计职称',
        name: '中级会计',
        courseName: '中级会计',
        en: 'zjkj',
        short: '中级',
        color: '#ff9800',
        icon: '中',
        iconfont: '#iconzhong',
        profession_id: '6',
        planName: '中级会计'
      },
      3: {
        title: '注册会计师',
        name: '注册会计',
        courseName: 'CPA课程',
        en: 'cpa',
        short: '注会',
        color: '#9c27b0',
        icon: '注',
        iconfont: '#iconzhu',
        profession_id: '7',
        planName: '注册会计师'
      },
      4: {
        title: '税务师',
        name: '税务师',
        courseName: '税务师',
        en: 'cta',
        short: '税务师',
        color: '#8bc34a',
        icon: '税',
        iconfont: '#iconshui',
        profession_id: '22',
        planName: '税务师'
      },
      5: {
        title: '会计实操',
        name: '会计实操',
        courseName: '实操课程',
        en: 'kjsc',
        color: '#5677fc',
        icon: '实',
        short: '实操',
        iconfont: '#iconshi',
        profession_id: '21'
      },
      6: {
        //新加专业
        title: '管理会计初级',
        name: '管理会计',
        courseName: '管理会计',
        en: 'mat_g',
        color: '#5677fc',
        icon: '管',
        short: '管会',
        iconfont: '#iconguan',
        profession_id: '23',
        planName: '管理会计师'
      },
      7: {
        title: '中级经济师',
        name: '中级经济师',
        courseName: '中级经济师',
        en: 'zjjjs',
        color: '#5677fc',
        icon: '中',
        short: '中级经济',
        //TOOD 换图标
        iconfont: '#iconjing',
        profession_id: '24',
        planName: '中级经济师'
      },
      8: {
        title: 'CMA',
        name: 'CMA',
        courseName: 'CMA',
        en: 'cma',
        color: '#5677fc',
        icon: 'C',
        short: 'CMA',
        //TOOD 换图标
        iconfont: '#iconcma',
        profession_id: '57',
        planName: 'CMA'
      }
    },
    submenu_map: {
      1: { title: '考试动态', en: 'dynamic' },
      2: { title: '考试指南', en: 'guide' },
      3: { title: '考试政策', en: 'policy' },
      4: { title: '行业动态', en: 'infos' },
      5: { title: '考试教材', en: 'book' },
      // 6: { title: "推荐课程", en: "course" },
      6: { title: '热门活动', en: 'course' },
      7: { title: '名师指导', en: 'learn' },
      8: { title: '备考经验', en: 'experience' },
      9: { title: '考试干货', en: 'stuff' }
    },
    specialTopicList: []
  }),
  mutations: {
    setBanner(state, data) {
      state.banners = data.data
    },
    setProfessionData(state, data) {
      let important = data.data.important || []
      important.forEach(function(item) {
        state.kjsc_menus.forEach(function(item1) {
          if (+item.nav_id === +item1.nav_id) {
            item.name = item1.title
          }
        })
      }) // 获取所有同学的评论
      data.data.important = important
      state.professionData = data.data
    },
    setShicaoRecommend(state, data) {
      state.shicaoRecommendList = data.data
    },
    setProfessionNews(state, data) {
      data.data.forEach(professionDataItem => {
        switch (professionDataItem.nav_id) {
          // 热点干货
          case 10:
            state.infos.list = professionDataItem.list || []
            break
          // 财税政策
          case 11:
            state.policy.list = professionDataItem.list || []
            break
          // 会计实务

          case 18:
            state.kjsw.list = professionDataItem.list || []
            break
          // 就业指导

          case 19:
            state.employment.list = professionDataItem.list || []
            break
          // 就业指导

          case 20:
            state.answer.list = professionDataItem.list || []
            break
        }
      })
    },
    setResearchCenter(state, data) {
      state.researchCenterList = data.data || []
    },
    setSpecialTopicList(state, data) {
      state.specialTopicList = data.data || []
    }
  },
  actions: {
    // 获取banner广告
    async getBanner({ state, commit, rootState }, params) {
      return scBanner()
        .then(res => {
          let { errcode, data } = res
          if (+errcode === 0 && data.ad && Array.isArray(data.ad.sc_lb)) {
            commit('setBanner', { data: data.ad })
          }
          return 1
        })
        .catch(() => {
          return 0
        })
    },
    //获取相关专业下重要资讯，免费试听，评论等
    async getProfessionData({ state, commit, rootState }, params) {
      return professionData({ profession_id: 5 })
        .then(res => {
          let { errcode, data } = res
          if (+errcode === 0 && res.data) {
            commit('setProfessionData', { data: data })
          }
          return 1
        })
        .catch(() => {
          return 0
        })
    },
    //获取实操学院的推荐商品
    async getShicaoRecommend({ state, commit, rootState }, params) {
      return shicaoRecommend()
        .then(res => {
          let { errcode, data } = res
          if (+errcode === 0 && Array.isArray(res.data)) {
            commit('setShicaoRecommend', { data: data })
          }
          return 1
        })
        .catch(() => {
          return 0
        })
    },
    // 获取实操资讯
    async getProfessionNews({ state, commit, rootState }, params) {
      return professionNews(params)
        .then(res => {
          let { errcode, data } = res
          if (+errcode === 0 && Array.isArray(res.data)) {
            commit('setProfessionNews', { data: data })
          }
          return 1
        })
        .catch(() => {
          return 0
        })
    },
    // 获取考证中心数据
    async getResearchCenter({ state, commit, rootState }, params) {
      return researchCenter(params)
        .then(res => {
          let { errcode, data } = res
          if (+errcode === 0 && Array.isArray(res.data)) {
            let { subjects, submenu_map } = state
            //过滤会计实操专业
            res.data = res.data.filter(function(item) {
              return +item.profession_id !== 5
            })
            var connecturl = function(profession_id, nav_id, id) {
              //拼接地址
              let path_string = ''

              for (var el1 in subjects) {
                if (+profession_id === +el1 && subjects[el1]) {
                  path_string += '/' + subjects[el1].en
                }
              }

              for (var el2 in submenu_map) {
                if (+nav_id === +el2 && submenu_map[el2]) {
                  path_string += '/' + submenu_map[el2].en
                }
              }

              return path_string + '_detail/' + id + '.html'
            }
            res.data.forEach(function(item, index) {
              if (subjects[item.profession_id]) {
                item.path = subjects[item.profession_id].en
                item.name = subjects[item.profession_id].name
              }

              item.news_list.forEach(function(nitem, nindex) {
                nitem.jump_path = connecturl(
                  nitem.profession_id,
                  nitem.nav_id,
                  nitem.id
                )
                Object.keys(submenu_map).forEach(function(item1, index2) {
                  if (+item1 === +nitem.nav_id) {
                    nitem.name = submenu_map[item1].title
                  }
                })
              })
            })
            commit('setResearchCenter', { data: data })
          }
          return 1
        })
        .catch(() => {
          return 0
        })
    },
    // 获取专题列表
    async getSpecialTopicList({ state, commit, rootState }, params) {
      return specialTopicList({ profession_id: '5' })
        .then(res => {
          let { errcode, data } = res
          if (+errcode === 0 && Array.isArray(res.data)) {
            let { subjects, submenu_map } = state
            var connecturl = function(profession_id, nav_id, id) {
              //拼接地址
              let path_string = ''

              for (var el1 in subjects) {
                if (+profession_id === +el1 && subjects[el1]) {
                  path_string += '/' + subjects[el1].en
                }
              }

              for (var el2 in submenu_map) {
                if (+nav_id === +el2 && submenu_map[el2]) {
                  path_string += '/' + submenu_map[el2].en
                }
              }

              return path_string + '_detail/' + id + '.html'
            }
            res.data.forEach(topic => {
              topic.news.forEach(newsItem => {
                newsItem.jump_path = connecturl(
                  newsItem.profession_id,
                  newsItem.nav_id,
                  newsItem.object_id
                )
              })
            })
            commit('setSpecialTopicList', { data: data })
          }
          return 1
        })
        .catch(() => {
          return 0
        })
    }
  },
  getters: {}
}
